package com.youlin.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.JmsTransactionManager;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.backoff.FixedBackOff;

import javax.jms.*;

@Configuration
@EnableJms
public class ActiveMqConfig {

//    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
                "admin", "admin", "tcp://192.168.182.123:61616");
//        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
//                "admin", "admin", "nio://192.168.182.123:61618");
//        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
//                "admin", "admin", "tcp://192.168.182.123:61608");
//        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
//                "admin", "admin", "nio://192.168.182.123:61608");
//        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("admin", "admin",
//                "failover:(tcp://192.168.182.123:61616,tcp://192.168.182.123:61617,tcp://192.168.182.123:61618)?randomize=false");
        activeMQConnectionFactory.setUseAsyncSend(true);
        activeMQConnectionFactory.setMessagePrioritySupported(true);
        RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
        redeliveryPolicy.setMaximumRedeliveries(4);
        redeliveryPolicy.setInitialRedeliveryDelay(2000);
        redeliveryPolicy.setMaximumRedeliveryDelay(5000);
        redeliveryPolicy.setUseExponentialBackOff(true);
        redeliveryPolicy.setBackOffMultiplier(2);
        activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);

//        activeMQConnectionFactory.setOptimizeAcknowledge(true);
//        activeMQConnectionFactory.setOptimizeAcknowledgeTimeOut(888);
        return activeMQConnectionFactory;
    }

//    @Bean
//    @Scope("prototype")
//    @DependsOn("activeMQConnectionFactory")
    public Connection connection(ActiveMQConnectionFactory activeMQConnectionFactory) throws JMSException {
        return activeMQConnectionFactory.createConnection();
    }

//    @Bean
    public JmsTransactionManager jmsTransactionManager(ConnectionFactory connectionFactory) {
        return new JmsTransactionManager(connectionFactory);
    }

//    @Bean
    public JmsMessagingTemplate jmsMessagingTemplate(ActiveMQConnectionFactory connectionFactory){
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
//        jmsTemplate.setSessionTransacted(true);
        return new JmsMessagingTemplate(jmsTemplate);
    }

    @Value("${myqueue}")
    private String myQueue ;
    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myQueue);
    }
//    @Bean
//    @DependsOn({"activeMQConnectionFactory", "jmsTransactionManager"})
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory activeMQConnectionFactory, JmsTransactionManager jmsTransactionManager) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(false);
        bean.setMaxMessagesPerTask(5);
        bean.setConnectionFactory(activeMQConnectionFactory);
//        bean.setSessionTransacted(true);
//        bean.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
//        FixedBackOff fixedBackOff = new FixedBackOff(2000, 3);
//        bean.setBackOff(fixedBackOff);
        bean.setTransactionManager(jmsTransactionManager);
        return bean;
    }

    @Value("${mytopic}")
    private String mytopic ;
    @Bean
    public Topic topic(){
        return new ActiveMQTopic(mytopic);
    }
//    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

}
